package com.whq.service.weixin.impl;

import com.alibaba.fastjson.JSON;
import com.whq.entity.Member;
import com.whq.service.MarketingService;
import com.whq.service.MemberService;
import com.whq.service.weixin.EventsService;
import com.whq.weixin.entity.WeChatReceiveMsg;
import com.whq.weixin.entity.WxBtnMsgBack;
import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 关注事件
 * @author 海易出行
 *
 */
@Service
public class SubscribeEventsServiceImpl implements EventsService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	MemberService memberService;
	
	@Autowired
	MarketingService marketingService;
	@Override
	public String response(WeChatReceiveMsg msg) throws Exception {
		System.out.println(JSON.toJSONString(msg));
		Member member = new Member();
		member.setOpenid(msg.getFromUserName());
		member.setName("1234");
		member.setCreated(new Date());
//		memberService.insertSelective(member);
		String message = null;

		return focusOnPush(msg,message);
	}

	/**
	 * 关注的时候推送
	 * @param msg
	 * @return
	 */
	public String focusOnPush(WeChatReceiveMsg msg,String messageStr) throws Exception {
		String xml = "ok";

		this.logger.info("关注的时候发送链接消息。。。。。。。。。。。。。。");

		String message = "欢迎登陆";
		if(StringUtils.isNotEmpty(messageStr)){
			message = messageStr;
		}
		WxBtnMsgBack wxmsg = new WxBtnMsgBack();
		wxmsg.setToUserName(msg.getFromUserName());
		wxmsg.setFromUserName(msg.getToUserName());
		wxmsg.setCreateTime(System.currentTimeMillis()/1000/60/60+"");
		wxmsg.setMsgType("text");
		wxmsg.setContent(message);

		//hwInfo.setDiskInfos(diskInfos);

		XStream xStream = new XStream();
		xStream.autodetectAnnotations(true);
		xml = xStream.toXML(wxmsg);
		this.logger.info("发送的报文为>>>>>>>：" + xml);
		return xml;
	}




}
